# 使用 ccache
find_program(CCACHE_PROGRAM ccache)
if(CCACHE_PROGRAM)
    set(CMAKE_C_COMPILER_LAUNCHER ${CCACHE_PROGRAM})
    set(CMAKE_CXX_COMPILER_LAUNCHER ${CCACHE_PROGRAM})
endif()

# 设置 C++17 标准
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

# 添加 GCC 版本检查
if(CMAKE_COMPILER_IS_GNUCXX)
    if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 7.0)
        message(FATAL_ERROR "GCC version must be at least 7.0 for C++17 support!")
    endif()
endif()

# 基础编译标志
set(SOURCE_DATE_EPOCH $ENV{SOURCE_DATE_EPOCH})
if(NOT SOURCE_DATE_EPOCH)
    string(TIMESTAMP SOURCE_DATE_EPOCH "%s" UTC)
endif()

# 获取Git Hash
execute_process(
    COMMAND git rev-parse --short HEAD
    WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
    OUTPUT_VARIABLE GIT_HASH
    OUTPUT_STRIP_TRAILING_WHITESPACE
    ERROR_QUIET
)
if(NOT GIT_HASH)
    set(GIT_HASH "unknown")
endif()

# 基础编译标志
set(CMAKE_COMMON_FLAGS "${CMAKE_COMMON_FLAGS} -DSOURCE_DATE_EPOCH=${SOURCE_DATE_EPOCH}")
set(CMAKE_COMMON_FLAGS "${CMAKE_COMMON_FLAGS} -DGIT_HASH=\\\"${GIT_HASH}\\\"")
set(CMAKE_COMMON_FLAGS "${CMAKE_COMMON_FLAGS} -Wall -Wextra -Werror -Wshadow -Wdate-time -Wno-psabi -Wno-format-nonliteral")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-rtti")

# debug 模式下添加更多调试选项
if(CMAKE_BUILD_TYPE STREQUAL "debug")
    set(CMAKE_COMMON_FLAGS "${CMAKE_COMMON_FLAGS} -g -O1 -rdynamic -fstack-protector-all -fno-omit-frame-pointer -DDEBUG")
    set(CMAKE_COMMON_FLAGS "${CMAKE_COMMON_FLAGS} -ftrapv -fno-inline -fno-eliminate-unused-debug-types")
    set(CMAKE_COMMON_FLAGS "${CMAKE_COMMON_FLAGS} -fstack-check -fstack-protector-strong")
    set(CMAKE_COMMON_FLAGS "${CMAKE_COMMON_FLAGS} -Wformat=2 -Wformat-security -Werror=format-security")
    # set(CMAKE_COMMON_FLAGS "${CMAKE_COMMON_FLAGS} -D_FORTIFY_SOURCE=2")
    # set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=address -fsanitize-recover=address -static-libasan")
    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-z,relro,-z,now -rdynamic")
    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-z,noexecstack")
else()
    set(CMAKE_COMMON_FLAGS "${CMAKE_COMMON_FLAGS} -Os -DNDEBUG")
    set(CMAKE_COMMON_FLAGS "${CMAKE_COMMON_FLAGS} -ffunction-sections -fdata-sections")
    set(CMAKE_COMMON_FLAGS "${CMAKE_COMMON_FLAGS} -ffast-math -funroll-loops")
    set(CMAKE_COMMON_FLAGS "${CMAKE_COMMON_FLAGS} -fomit-frame-pointer")
    set(CMAKE_COMMON_FLAGS "${CMAKE_COMMON_FLAGS} -fstack-protector-strong")
    
    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections")
    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-O1")
    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--as-needed")
    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -s")
    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-z,noexecstack")
endif()

# 设置输出目录
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/out/bin/)

# 系统三方库
set(EL_SYS_LIBS dl pthread atomic udev z ssl crypto ffi pcre2-8 minizip-ng swresample avutil avcodec avformat curl uv mongoose freetype eXosip2 osipparser2 osip2 websockets)

# GLib相关库（按依赖层次排序）
set(EL_GLIB_LIBS 
    gio-2.0 
    gobject-2.0 
    gmodule-2.0 
    glib-2.0
    gthread-2.0
)

# GStreamer相关库（按依赖层次排序）
set(EL_GSTREAMER_LIBS 
    gstrtspserver-1.0
    gstwebrtc-1.0
    gstwebrtcnice-1.0
    nice
    gstvideo-1.0
    gstaudio-1.0
    gstpbutils-1.0
    gstrtsp-1.0
    gstrtp-1.0
    gstsdp-1.0
    gstapp-1.0
    gsttag-1.0
    gstnet-1.0
    gstbase-1.0
    gstreamer-1.0
)

find_path(GSTREAMER_INCLUDE_DIR gst/gst.h HINTS ${CMAKE_SYSROOT}/usr/include/gstreamer-1.0)
find_path(GLIB_INCLUDE_DIR glib.h HINTS ${CMAKE_SYSROOT}/usr/include/glib-2.0)
find_path(GLIBCONFIG_INCLUDE_DIR glibconfig.h HINTS ${CMAKE_SYSROOT}/usr/lib/glib-2.0/include)
find_path(GSTWEBRTC_INCLUDE_DIR gst/webrtc/webrtc.h HINTS ${CMAKE_SYSROOT}/usr/include/gstreamer-1.0)
find_path(GSTAPP_INCLUDE_DIR gst/app/gstappsrc.h HINTS ${CMAKE_SYSROOT}/usr/include/gstreamer-1.0)
find_path(GSTRTSP_INCLUDE_DIR gst/rtsp-server/rtsp-server.h HINTS ${CMAKE_SYSROOT}/usr/include/gstreamer-1.0)

include_directories(${GSTREAMER_INCLUDE_DIR})
include_directories(${GLIB_INCLUDE_DIR})
include_directories(${GLIBCONFIG_INCLUDE_DIR})
include_directories(${GSTWEBRTC_INCLUDE_DIR})
include_directories(${GSTAPP_INCLUDE_DIR})
include_directories(${GSTRTSP_INCLUDE_DIR})

# 所有模块的对外头文件统一目录
include_directories(${PROJECT_SOURCE_DIR}/third_party/include)

include_directories(${PROJECT_SOURCE_DIR}/common/utils/include)

include_directories(${PROJECT_SOURCE_DIR}/hal/mpp/include)
include_directories(${PROJECT_SOURCE_DIR}/hal/ai/include)

include_directories(${PROJECT_SOURCE_DIR}/base/common/include)
include_directories(${PROJECT_SOURCE_DIR}/base/network/include)
include_directories(${PROJECT_SOURCE_DIR}/base/peripheral/include)
include_directories(${PROJECT_SOURCE_DIR}/base/muxer/include)
include_directories(${PROJECT_SOURCE_DIR}/base/media/include)
include_directories(${PROJECT_SOURCE_DIR}/base/storage/include)
include_directories(${PROJECT_SOURCE_DIR}/base/upgrade/include)
include_directories(${PROJECT_SOURCE_DIR}/base/system_service/include)
include_directories(${PROJECT_SOURCE_DIR}/base/web_server/include)
include_directories(${PROJECT_SOURCE_DIR}/base/file_manager/include)
include_directories(${PROJECT_SOURCE_DIR}/base/record/include)
include_directories(${PROJECT_SOURCE_DIR}/base/algo/include)
include_directories(${PROJECT_SOURCE_DIR}/base/discovery/include)
include_directories(${PROJECT_SOURCE_DIR}/base/event/include)

include_directories(${PROJECT_SOURCE_DIR}/foundation/xiaozhi/include)
include_directories(${PROJECT_SOURCE_DIR}/foundation/stream_service/include)
include_directories(${PROJECT_SOURCE_DIR}/foundation/aliyun/include)
include_directories(${PROJECT_SOURCE_DIR}/foundation/restful/include)
include_directories(${PROJECT_SOURCE_DIR}/foundation/onvif/include)
include_directories(${PROJECT_SOURCE_DIR}/foundation/gb28181/include)
include_directories(${PROJECT_SOURCE_DIR}/foundation/protocol/include)


set(EL_BASE_LIBS utils common)
set(EL_HAL_LIBS hal_mpp hal_ai)
set(EL_FDN_LIBS network onvif xiaozhi aliyun algo file_manager protocol restful upgrade fdn_muxer record storage web_server stream_service stream system_service event discovery peripheral gb28181)

# 所有库的集合
set(EL_ALL_LIBS
    ${EL_SYS_LIBS}
    ${EL_GLIB_LIBS}
    ${EL_GSTREAMER_LIBS}
    ${EL_CHIP_LIBS}
    ${EL_BASE_LIBS}
    ${EL_HAL_LIBS}
    ${EL_FDN_LIBS}
    ${EL_VENDOR_LIBS}
)